package com.sunshine.user.service.impl;

import com.sunshine.user.dto.UserRegisterDTO;
import com.sunshine.user.entity.Merchant;
import com.sunshine.user.entity.Users;
import com.sunshine.user.mapper.MerchantDao;
import com.sunshine.user.service.MerchantService;
import org.redisson.api.RedissonClient;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import sunshine.commcon.util.JwtUtil;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Resource
    MerchantDao merchantDao;

    @Resource
    RedissonClient redissonClient;

    @Override
    public Result<Boolean> createMerchant(Merchant merchant) {
        try {
            //查看用户名是否已存在
            Merchant selectMerchant = merchantDao.selectMerchant(merchant.getMerchantName());
            if (selectMerchant != null) {
                return Result.fail("用户名已经存在");
            }
            //设置随机ID
            long randomId = ThreadLocalRandom.current().nextLong(100000, 10000000000L);
            merchant.setId(randomId);
            //插入用户
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            merchant.setMerchantPassword(bCryptPasswordEncoder.encode(merchant.getMerchantPassword()));
            merchant.setCreateDate(new Date()); // 添加创建时间
            merchantDao.insert(merchant);
            return Result.ok(true);
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈，便于调试
            return Result.fail("系统异常");
        }
    }

    @Override
    public Result<Merchant> login(String merchantName, String merchantPassword) {

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //查找是否有该用户
        Merchant selectMerchant = merchantDao.selectMerchant(merchantName);
        if (selectMerchant != null) {
            //验证密码
            if (bCryptPasswordEncoder.matches(merchantPassword, selectMerchant.getMerchantPassword())) {
                // 使用商家自己的用户名生成token，而不是users.getUsername()
                String token = JwtUtil.createToken(selectMerchant.getId(), selectMerchant.getMerchantName());
                selectMerchant.setToken(token);
                redissonClient.getBucket("user_token:" + selectMerchant.getId()).set(token, 24, TimeUnit.HOURS);
                return Result.ok(selectMerchant);
            }

        }
        return Result.fail("用户名或密码错误");

    }
    
    @Override
    public Result<Boolean> updatePassword(Long merchantId, String oldPassword, String newPassword) {
        try {
            // 获取商家信息
            Merchant merchant = merchantDao.queryById(merchantId);
            if (merchant == null) {
                return Result.fail("商家不存在");
            }
            
            // 验证原密码
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            if (!bCryptPasswordEncoder.matches(oldPassword, merchant.getMerchantPassword())) {
                return Result.fail("原密码错误");
            }
            
            // 更新密码
            merchant.setMerchantPassword(bCryptPasswordEncoder.encode(newPassword));
            merchantDao.update(merchant);
            
            return Result.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }
}